/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaPersona.java
 *
 * Created on 24/07/2011, 12:34:39 AM
 */
package co.srm.vistas.funcionales;

import co.srm.entidades.Cliente;
import co.srm.entidades.Cobrador;
import co.srm.entidades.DatosFinanciero;
import co.srm.entidades.Pago;
import co.srm.entidades.Persona;
import co.srm.entidades.Prestamo;
import co.srm.entidades.Prestamo.ModoPago;
import co.srm.manejadores.ManejadorCliente;
import co.srm.manejadores.ManejadorCobrador;
import co.srm.manejadores.ManejadorDatosFinancieros;
import co.srm.manejadores.ManejadorPersona;
import co.srm.manejadores.ManejadorPrestamo;
import co.srm.manejadores.ManejadorUsuario;
import co.srm.utilidades.Control;
import co.srm.vistas.VentanaPrincipal;
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author JORGE
 */
public class VentanaPrestamo extends javax.swing.JDialog {

    public static Cliente clienteSt = null;
    ManejadorPrestamo mP;
    private Prestamo prestamoEdit = null;
    public static int origenVentanaPrestamo = 0;

    /** Creates new form VentanaPersona */
    public VentanaPrestamo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        inicializar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXPanel3 = new org.jdesktop.swingx.JXPanel();
        jLabel13 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jLabel1 = new javax.swing.JLabel();
        campoFecha = new org.jdesktop.swingx.JXDatePicker();
        jLabel12 = new javax.swing.JLabel();
        comboCobrador = new javax.swing.JComboBox();
        panelCliente = new org.jdesktop.swingx.JXPanel();
        buscarCliente = new javax.swing.JButton();
        campoCedula = new javax.swing.JTextField();
        labelCedula = new javax.swing.JLabel();
        campoNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        campoTelefono = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        areaDireccion = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        clientesList = new javax.swing.JButton();
        campoIdCliente = new javax.swing.JTextField();
        jXPanel2 = new org.jdesktop.swingx.JXPanel();
        modificarPrestamo = new javax.swing.JButton();
        guardarPrestamo = new javax.swing.JButton();
        jLabel15 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        idUltimoPrestamo = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        areaNotificacion = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        botonEliminar = new javax.swing.JButton();
        panelPrestamo = new org.jdesktop.swingx.JXPanel();
        labelObservacion = new javax.swing.JLabel();
        labelMonto = new javax.swing.JLabel();
        campoMontoPagar = new javax.swing.JTextField();
        labelDias = new javax.swing.JLabel();
        campoDias = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        campoValorDia = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        campoMonto = new javax.swing.JTextField();
        labelPorcentaje = new javax.swing.JLabel();
        campoPorcentaje = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        comboTipoPago = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        areaObservacion = new javax.swing.JTextArea();
        labelCodigoPrestamo = new javax.swing.JLabel();
        campoCodigo = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jXPanel3.setBackground(new java.awt.Color(255, 220, 98));
        jXPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 1, true));
        jXPanel3.setForeground(new java.awt.Color(255, 255, 255));

        jLabel13.setFont(new java.awt.Font("Arial", 3, 18));
        jLabel13.setText("Registro De Prestamos");

        javax.swing.GroupLayout jXPanel3Layout = new javax.swing.GroupLayout(jXPanel3);
        jXPanel3.setLayout(jXPanel3Layout);
        jXPanel3Layout.setHorizontalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel3Layout.createSequentialGroup()
                .addGap(255, 255, 255)
                .addComponent(jLabel13)
                .addContainerGap(334, Short.MAX_VALUE))
        );
        jXPanel3Layout.setVerticalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel13)
                .addContainerGap(7, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jXPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Datos Generales", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel1.setText("Fecha ");

        jLabel12.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel12.setText("Cobrador");

        comboCobrador.setFont(new java.awt.Font("Arial", 0, 12));
        comboCobrador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCobradorActionPerformed(evt);
            }
        });

        panelCliente.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)), "Cliente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        buscarCliente.setFont(new java.awt.Font("Arial", 1, 12));
        buscarCliente.setText("Buscar");
        buscarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarClienteActionPerformed(evt);
            }
        });

        campoCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoCedulaActionPerformed(evt);
            }
        });

        labelCedula.setFont(new java.awt.Font("Arial", 0, 12));
        labelCedula.setText("Cedula");

        campoNombre.setEditable(false);

        jLabel2.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel2.setText("Nombre");

        campoTelefono.setEditable(false);

        jLabel4.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel4.setText("Telefono");

        areaDireccion.setColumns(20);
        areaDireccion.setEditable(false);
        areaDireccion.setRows(5);
        jScrollPane1.setViewportView(areaDireccion);

        jLabel6.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel6.setText("Direccion");

        clientesList.setFont(new java.awt.Font("Arial", 1, 12));
        clientesList.setText("Clientes");
        clientesList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clientesListActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelClienteLayout = new javax.swing.GroupLayout(panelCliente);
        panelCliente.setLayout(panelClienteLayout);
        panelClienteLayout.setHorizontalGroup(
            panelClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelClienteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelClienteLayout.createSequentialGroup()
                        .addComponent(labelCedula)
                        .addGap(23, 23, 23)
                        .addComponent(campoCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(buscarCliente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoIdCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(clientesList))
                    .addGroup(panelClienteLayout.createSequentialGroup()
                        .addGroup(panelClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel6))
                        .addGap(11, 11, 11)
                        .addGroup(panelClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(panelClienteLayout.createSequentialGroup()
                                .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1))))
                .addContainerGap(10, Short.MAX_VALUE))
        );
        panelClienteLayout.setVerticalGroup(
            panelClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelClienteLayout.createSequentialGroup()
                .addGroup(panelClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buscarCliente)
                    .addComponent(labelCedula)
                    .addComponent(campoCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoIdCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(clientesList))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(campoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(campoFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addComponent(jLabel12)
                        .addGap(18, 18, 18)
                        .addComponent(comboCobrador, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panelCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(campoFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(comboCobrador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addComponent(panelCliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jXPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        modificarPrestamo.setFont(new java.awt.Font("Arial", 1, 12));
        modificarPrestamo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/srm/vistas/iconos/modificar.png"))); // NOI18N
        modificarPrestamo.setText("Modificar");
        modificarPrestamo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarPrestamoActionPerformed(evt);
            }
        });

        guardarPrestamo.setFont(new java.awt.Font("Arial", 1, 12));
        guardarPrestamo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/srm/vistas/iconos/guardaricono.png"))); // NOI18N
        guardarPrestamo.setText("Guardar");
        guardarPrestamo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarPrestamoActionPerformed(evt);
            }
        });

        jLabel15.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/srm/vistas/iconos/SRM_logo_66x66.png"))); // NOI18N
        jLabel15.setBorder(javax.swing.BorderFactory.createEtchedBorder(java.awt.Color.lightGray, java.awt.Color.lightGray));

        jLabel5.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel5.setText("Ultimo Prestamo");

        idUltimoPrestamo.setEditable(false);
        idUltimoPrestamo.setFont(new java.awt.Font("Tahoma", 1, 12));

        areaNotificacion.setColumns(20);
        areaNotificacion.setRows(5);
        jScrollPane3.setViewportView(areaNotificacion);

        jLabel7.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel7.setText("Novedad");

        botonEliminar.setFont(new java.awt.Font("Arial", 1, 12));
        botonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/srm/vistas/iconos/Delete-icon.png"))); // NOI18N
        botonEliminar.setText("Eliminar");
        botonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jXPanel2Layout = new javax.swing.GroupLayout(jXPanel2);
        jXPanel2.setLayout(jXPanel2Layout);
        jXPanel2Layout.setHorizontalGroup(
            jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel15)
                        .addGap(35, 35, 35))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel2Layout.createSequentialGroup()
                        .addComponent(idUltimoPrestamo, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jXPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jXPanel2Layout.createSequentialGroup()
                        .addComponent(guardarPrestamo, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel2Layout.createSequentialGroup()
                        .addComponent(modificarPrestamo, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel2Layout.createSequentialGroup()
                        .addComponent(botonEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        jXPanel2Layout.setVerticalGroup(
            jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel2Layout.createSequentialGroup()
                .addComponent(jLabel15)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(idUltimoPrestamo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(guardarPrestamo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(modificarPrestamo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botonEliminar)
                .addGap(16, 16, 16))
        );

        panelPrestamo.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Detalle Prestamo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        labelObservacion.setFont(new java.awt.Font("Arial", 0, 12));
        labelObservacion.setText("Observacion");

        labelMonto.setFont(new java.awt.Font("Arial", 0, 12));
        labelMonto.setText("Monto $");

        campoMontoPagar.setEditable(false);
        campoMontoPagar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoMontoPagarActionPerformed(evt);
            }
        });
        campoMontoPagar.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                campoMontoPagarInputMethodTextChanged(evt);
            }
        });

        labelDias.setFont(new java.awt.Font("Arial", 0, 12));
        labelDias.setText("Dias");

        campoDias.setFont(new java.awt.Font("Arial", 0, 12));
        campoDias.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                campoDiasFocusLost(evt);
            }
        });
        campoDias.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoDiasKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                campoDiasKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                campoDiasKeyTyped(evt);
            }
        });

        jLabel19.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel19.setText("Valor Dia $");

        campoValorDia.setEditable(false);

        jLabel20.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel20.setText("Monto A Pagar");

        campoMonto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoMontoKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                campoMontoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                campoMontoKeyTyped(evt);
            }
        });

        labelPorcentaje.setFont(new java.awt.Font("Arial", 0, 12));
        labelPorcentaje.setText("Porcentaje %");

        campoPorcentaje.setText("30");
        campoPorcentaje.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                campoPorcentajeKeyReleased(evt);
            }
        });

        jLabel22.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel22.setText("Tipo de Pago");

        comboTipoPago.setFont(new java.awt.Font("Arial", 0, 14));

        areaObservacion.setColumns(20);
        areaObservacion.setRows(5);
        jScrollPane2.setViewportView(areaObservacion);

        labelCodigoPrestamo.setFont(new java.awt.Font("Arial", 0, 12));
        labelCodigoPrestamo.setText("Codigo P.");

        campoCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoCodigoActionPerformed(evt);
            }
        });
        campoCodigo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                campoCodigoFocusLost(evt);
            }
        });
        campoCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoCodigoKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                campoCodigoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                campoCodigoKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout panelPrestamoLayout = new javax.swing.GroupLayout(panelPrestamo);
        panelPrestamo.setLayout(panelPrestamoLayout);
        panelPrestamoLayout.setHorizontalGroup(
            panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPrestamoLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelPrestamoLayout.createSequentialGroup()
                        .addComponent(labelObservacion)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelPrestamoLayout.createSequentialGroup()
                        .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelPorcentaje)
                            .addComponent(labelMonto)
                            .addComponent(labelDias)
                            .addComponent(labelCodigoPrestamo))
                        .addGap(18, 18, 18)
                        .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(campoCodigo)
                            .addComponent(campoMonto, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                            .addComponent(campoDias, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(campoPorcentaje, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                        .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel20)
                            .addComponent(jLabel22)
                            .addComponent(jLabel19))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(comboTipoPago, 0, 138, Short.MAX_VALUE)
                            .addComponent(campoMontoPagar)
                            .addComponent(campoValorDia))
                        .addGap(54, 54, 54)))
                .addContainerGap())
        );
        panelPrestamoLayout.setVerticalGroup(
            panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPrestamoLayout.createSequentialGroup()
                .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCodigoPrestamo)
                    .addComponent(campoCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelPrestamoLayout.createSequentialGroup()
                        .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelMonto)
                            .addComponent(campoMonto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelPorcentaje)
                            .addComponent(campoPorcentaje, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(8, 8, 8)
                        .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(labelDias)
                            .addComponent(campoDias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(panelPrestamoLayout.createSequentialGroup()
                        .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(comboTipoPago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel22))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(campoMontoPagar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel20))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(campoValorDia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel19))))
                .addGap(18, 18, 18)
                .addGroup(panelPrestamoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelObservacion)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelPrestamo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jXPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jXPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(panelPrestamo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void guardarPrestamoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarPrestamoActionPerformed
        // TODO add your handling code here:
        Cliente cli = clienteSt;
        boolean guardo = false;
        boolean tieneVigente = false;
        // selecciono el tipo usuario
        if (cli != null) {
            if (cli.getPrestamos().size() > 0) {
                for (Prestamo p : cli.getPrestamos()) {
                    if (p.getEstadoActual().equals(Prestamo.EstadoActual.VIGENTE)) {
                        tieneVigente = true;
                        break;
                    }
                }
                if (tieneVigente) {
                    areaNotificacion.setForeground(Color.BLUE);
                    areaNotificacion.setText("El Cliente " + cli.getNombres() + "\n" + cli.getApellidos() + "\ntiene un prestamo\n VIGENTE");
                }

            }
        }

        if (comboCobrador.getSelectedIndex() != 0) {

            String codigo = campoCodigo.getText();
            String monto = campoMonto.getText();
            String plazo = campoDias.getText();
            String neto = campoMontoPagar.getText();
            String interes = campoPorcentaje.getText();
            String valorCuota = campoValorDia.getText();
            String observacion = areaObservacion.getText();
            Date fecha = new Date();
            String saldoActual = campoMontoPagar.getText();//inicialmente debe el monto que presto.
            Prestamo.EstadoActual estadoP = Prestamo.EstadoActual.VIGENTE;//el estado inicial es vigente
            Prestamo.ModoPago modo = (Prestamo.ModoPago) comboTipoPago.getSelectedItem();
            Cobrador cobrador = (Cobrador) comboCobrador.getSelectedItem();
            if (!codigo.isEmpty() && !monto.isEmpty() && !plazo.isEmpty() && !neto.isEmpty() && !interes.isEmpty()
                    && !valorCuota.isEmpty() && cli != null && cobrador != null) {
                guardo = mP.crarPrestamo(null, codigo, monto, plazo, neto, interes, valorCuota, fecha, saldoActual, observacion, estadoP, modo, cobrador, cli, false);
                if (guardo) {
                    if (ManejadorPrestamo.idLastPrestamo != null) {
                        Long idP = ManejadorPrestamo.idLastPrestamo;
                        idUltimoPrestamo.setText(String.valueOf(idP));
                        String msm = " Prestamo # " + idP;
                        JTextArea txtArea = new JTextArea(msm);
                        Font font = new Font("sansserif", Font.BOLD, 42);
                        txtArea.setFont(font);
                        txtArea.setForeground(Color.BLUE);
                        txtArea.setOpaque(false);
                        txtArea.setEditable(false);
                        txtArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
                        JOptionPane.showMessageDialog(null, txtArea, "Prestamo Registrado Exitosamente", JOptionPane.PLAIN_MESSAGE);
                        limpiar();
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Prestamo No Registrado", "Prestamo", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Verifique los valores obligatorios", "Prestamo", JOptionPane.ERROR_MESSAGE);
            }



        } else {
            JOptionPane.showMessageDialog(null, "Seleccione un cobrador primero", "Prestamo", JOptionPane.WARNING_MESSAGE);
        }


    }//GEN-LAST:event_guardarPrestamoActionPerformed

    private void comboCobradorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCobradorActionPerformed
        // TODO add your handling code here:
        if (comboCobrador.getSelectedIndex() == 0) {
            Control.disableAndEnabledContainer(panelCliente, false);
            Control.disableAndEnabledContainer(panelPrestamo, false);
            campoCodigo.setEnabled(true);
        } else if (comboCobrador.getSelectedIndex() >= 1) {
            Control.disableAndEnabledContainer(panelCliente, true);
            Control.disableAndEnabledContainer(panelPrestamo, true);
        }
    }//GEN-LAST:event_comboCobradorActionPerformed

    private void buscarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarClienteActionPerformed
        // TODO add your handling code here:
        if (!campoCedula.getText().isEmpty()) {
            ManejadorPersona mper = new ManejadorPersona();
            Persona persona = null;
            persona = mper.consultarPersonaByIdentificacion(campoCedula.getText());
            if (persona != null) {
                String tipopersona = persona.getClass().getSimpleName();
                if (tipopersona.equals("Cliente")) {
                    ManejadorCliente mC = new ManejadorCliente();
                    Cliente c = null;
                    String identificacion = campoCedula.getText().trim();
                    if (identificacion != null && !identificacion.isEmpty()) {
                        c = mC.consultarUsuarioByIdentificacion(identificacion);
                        if (c != null) {
                            clienteSt = c;
                            System.out.println(VentanaPrestamo.clienteSt.getNombres());
                            campoNombre.setText(c.getNombres() + " " + c.getApellidos());
                            campoTelefono.setText(c.getTelefono());
                            areaDireccion.setText(c.getDireccion());
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Ingrese una identificacion valida", "Busqueda De Cliente", JOptionPane.WARNING_MESSAGE);
                    }
                }
                if (tipopersona.equals("Cobrador")) {
                    JOptionPane.showMessageDialog(null, "Esta identificacion ya se encuentra en el sistema", "Busqueda De Cliente", JOptionPane.WARNING_MESSAGE);
                    limpiar();
                }
                if (tipopersona.equals("Usuario")) {
                    JOptionPane.showMessageDialog(null, "Esta identificacion ya se encuentra en el sistema", "Busqueda De Cliente", JOptionPane.WARNING_MESSAGE);
                    limpiar();
                }
            } else {
                // iii
//                JOptionPane.showMessageDialog(null, "Cliente no encontrado", "Busqueda De Cliente", JOptionPane.WARNING_MESSAGE);
                int r = JOptionPane.showConfirmDialog(this, "Cliente no encontrado Desea registrarlo , ó\nverifique la cedula del cliente", "No esta registrado", JOptionPane.YES_NO_OPTION);
                if (r == JOptionPane.YES_OPTION) {
                    VentanaPersona.idThePrestamo = campoCedula.getText();
                    VentanaPersona.isRegistroPrestamo = true;
                    new VentanaPersona(null, true).setVisible(true);
                }
            }
        } else {
//            Control.mensajeJOption(" Por favor ingrese una Identificacion", "Campos Vacios", 15);
        }
    }//GEN-LAST:event_buscarClienteActionPerformed

    private void campoMontoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoMontoKeyTyped
        // TODO add your handling code here:
        Control.validaNumeros(evt, campoMonto, 10);
    }//GEN-LAST:event_campoMontoKeyTyped

    private void campoMontoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoMontoKeyPressed
        //campoMontoKeyReleased(evt);
        if (!campoMonto.getText().equals("")) {
            //campoDiasKeyPressed(evt);
            campoDias.setText("30");
            campoDiasKeyReleased(evt);
        }



    }//GEN-LAST:event_campoMontoKeyPressed

    private void campoMontoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoMontoKeyReleased
        // TODO add your handling code here:
        int interes;
        int monto;
        if (!campoPorcentaje.getText().isEmpty()) {
            interes = Integer.parseInt(campoPorcentaje.getText());
//            System.out.println("interes = " + interes);
        } else {
            campoPorcentaje.setText("20");
            interes = Integer.parseInt(campoPorcentaje.getText());
//            System.out.println("interes2 = " + interes);
        }
        if (campoMonto.getText().isEmpty()) {
            monto = 0;
            campoDias.setText("0");
            campoValorDia.setText("0");
            campoMontoPagar.setText("0");
        } else {

            monto = Integer.parseInt(campoMonto.getText());
            campoDias.setText("30");
            campoDiasKeyReleased(evt);
            //}//descomentar
//        System.out.println("monto = " + monto);
            // concateno el 1.
            String porConv = "1." + String.valueOf(interes);
//              System.out.println("porcAdi = " + porConv);
            // lo convierto a float
            double porAdic = Double.valueOf(porConv);
            // ahora si hago la vuelta
            NumberFormat formatter = new DecimalFormat("#.#");
            int montoPagar = (int) Math.round(monto * porAdic);
            if (!campoDias.getText().isEmpty()) {
                int dias;
                dias = Integer.valueOf(campoDias.getText());
                int diasA = (int) Math.round(montoPagar / dias);
//                String totalcampoValorDia = formatter.format(diasA).replace(",", ".");
                String totalcampoValorDia = String.valueOf(diasA);
                campoValorDia.setText(totalcampoValorDia);
            }
//            String totalMostrar = formatter.format(montoPagar).replace(",", ".");
            String totalMostrar = String.valueOf(montoPagar);
            campoMontoPagar.setText(totalMostrar);
        }
//              System.out.println("totalMostrar = " + String.valueOf(totalMostrar));

    }//GEN-LAST:event_campoMontoKeyReleased

    private void campoMontoPagarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoMontoPagarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoMontoPagarActionPerformed

    private void campoDiasKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoDiasKeyTyped
        // TODO add your handling code here:
        Control.validaNumeros(evt, campoDias, 3);
    }//GEN-LAST:event_campoDiasKeyTyped

    private void campoDiasKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoDiasKeyReleased
        double dias;
        if (!campoMontoPagar.getText().isEmpty()) {
            double cantidadPagar = Double.valueOf(campoMontoPagar.getText());

            if (!campoDias.getText().equals("")) {

                dias = Double.valueOf(campoDias.getText());
                dias = (dias == 0) ? 30 : dias;

                int diasA = (int) Math.round(cantidadPagar / dias);
                campoValorDia.setText(String.valueOf(diasA));
            }
            if (campoDias.getText().equals("")) {
                campoValorDia.setText("");
            }
        }
    }//GEN-LAST:event_campoDiasKeyReleased

    private void campoDiasKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoDiasKeyPressed
        // TODO add your handling code here:
        campoDiasKeyReleased(evt);
    }//GEN-LAST:event_campoDiasKeyPressed

    private void campoMontoPagarInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_campoMontoPagarInputMethodTextChanged
        // TODO add your handling code here:
        if (!campoMontoPagar.getText().isEmpty()) {
            campoDias.setEditable(true);
            campoDias.setEnabled(true);
        } else {
            campoDias.setEditable(false);
            campoDias.setEnabled(false);
        }

    }//GEN-LAST:event_campoMontoPagarInputMethodTextChanged

    private void campoDiasFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_campoDiasFocusLost
        // TODO add your handling code here:
        if (!evt.isTemporary()) {
            if (campoDias.getText().equals("")) {
                campoDias.setText("30");
                campoDiasKeyReleased(null);
            }
        }
    }//GEN-LAST:event_campoDiasFocusLost

    private void clientesListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clientesListActionPerformed
        origenVentanaPrestamo = 1;
//        System.out.println("origenVentanaPrestamo = " + origenVentanaPrestamo);
        new BusquedaCliente(null, true).setVisible(true);

    }//GEN-LAST:event_clientesListActionPerformed

    private void campoCedulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoCedulaActionPerformed
        // TODO add your handling code here:
        buscarClienteActionPerformed(evt);
    }//GEN-LAST:event_campoCedulaActionPerformed

    private void campoPorcentajeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoPorcentajeKeyReleased
        int interes = 0;
        int monto;

        if (!campoPorcentaje.getText().isEmpty()) {
            interes = Integer.parseInt(campoPorcentaje.getText());
//            System.out.println("interes = " + interes);
        }

        if (campoMonto.getText().isEmpty()) {
            monto = 0;
            campoDias.setText("0");
            campoValorDia.setText("0");
            campoMontoPagar.setText("0");
        } else {
            monto = Integer.parseInt(campoMonto.getText());
            //campoDias.setText("30");
            // campoDiasKeyReleased(evt);
            //}//descomentar
//        System.out.println("monto = " + monto);
            // concateno el 1.
            String porConv = "1." + String.valueOf(interes);
//              System.out.println("porcAdi = " + porConv);
            // lo convierto a float
            double porAdic = Double.valueOf(porConv);
//              System.out.println("porAdic = " + porAdic);
            // ahora si hago la vuelta
            NumberFormat formatter = new DecimalFormat("#.#");
            int montoPagar = (int) Math.round(monto * porAdic);

            //String totalMostrar = formatter.format(montoPagar).replace(",", ".");
            campoMontoPagar.setText("");
            campoMontoPagar.setText(String.valueOf(montoPagar));

            campoDiasKeyReleased(evt);
        }
    }//GEN-LAST:event_campoPorcentajeKeyReleased

    private void campoCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoCodigoKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoCodigoKeyPressed

    private void campoCodigoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoCodigoKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_campoCodigoKeyReleased

    private void campoCodigoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoCodigoKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_campoCodigoKeyTyped

    private void campoCodigoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_campoCodigoFocusLost
        String codigo = campoCodigo.getText();
        if (!codigo.isEmpty()) {
            ManejadorPrestamo mPres = new ManejadorPrestamo();
            Prestamo prestamo = null;
            prestamo = mPres.consultarPrestamoByCodigo(codigo);
            System.out.println("prestamo = " + prestamo);
            if (prestamo != null) {
//                JOptionPane.showMessageDialog(null, "Ya se encuentra registrado un prestamo: " + "\n" + " Con el codigo: " + codigo, "Prestamo Ya Existe", JOptionPane.INFORMATION_MESSAGE);
//                campoCodigo.setText("");
//                campoCodigo.requestFocus();
                clienteSt = prestamo.getCliente();
                prestamoEdit = prestamo;

                /**Datos del cliente**/
                String nombre = prestamo.getCliente().getNombres() + " " + prestamo.getCliente().getApellidos();
                String telefonos = prestamo.getCliente().getTelefono() + "/" + prestamo.getCliente().getCelular();

                campoIdCliente.setText(prestamo.getCliente().getId().toString());
                campoCedula.setText(prestamo.getCliente().getIdentificacion().toString());
                campoNombre.setText(nombre);
                campoTelefono.setText(telefonos);
                areaDireccion.setText(prestamo.getCliente().getDireccion());
                /**Datos del cliente**/
                /**Datos del cobrador**/
                comboCobrador.setSelectedItem(prestamo.getCobrador());
                /**Datos del cobrador**/
                /**Detalle del prestamo**/
                int interes = prestamo.getInteres().intValue();

                campoFecha.setDate(prestamo.getFecha());
                campoMonto.setText(String.valueOf(prestamo.getMonto().intValue()));
                comboTipoPago.setSelectedItem(prestamo.getModoPago());
                campoPorcentaje.setText(String.valueOf(interes));
                campoMontoPagar.setText(String.valueOf(prestamo.getNeto().intValue()));
                campoDias.setText(prestamo.getPlazo().toString());
                campoValorDia.setText(String.valueOf(prestamo.getValorCuota().intValue()));
                areaObservacion.setText(prestamo.getObservacion());

                campoMontoPagar.setEnabled(false);
                campoValorDia.setEnabled(false);
                modificarPrestamo.setEnabled(true);
                guardarPrestamo.setEnabled(false);

//                Control.disableAndEnabledContainer(panelCliente, false);
                campoCedula.setEnabled(true);
                buscarCliente.setEnabled(true);
                clientesList.setEnabled(true);

                botonEliminar.setEnabled(true);
            } else {
                botonEliminar.setEnabled(false);
               // limpiar();
            }
        }
    }//GEN-LAST:event_campoCodigoFocusLost

    private void campoCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoCodigoActionPerformed
        campoCodigoFocusLost(null);
    }//GEN-LAST:event_campoCodigoActionPerformed

    private void modificarPrestamoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificarPrestamoActionPerformed
        String codigo = campoCodigo.getText();
        String monto = campoMonto.getText();
        String plazo = campoDias.getText();
        String neto = campoMontoPagar.getText();
        String interes = campoPorcentaje.getText();
        String valorCuota = campoValorDia.getText();
        String observacion = areaObservacion.getText();
        observacion = (observacion.isEmpty() ? "No Suministrada" : observacion);
        Date fecha = campoFecha.getDate();

        Prestamo.ModoPago modo = (Prestamo.ModoPago) comboTipoPago.getSelectedItem();

        Cobrador cobrador = (Cobrador) comboCobrador.getSelectedItem();

        prestamoEdit.setCliente(clienteSt);//actualizamos el cliente del prestamo
        prestamoEdit.setCobrador(cobrador);//actualizamos el cobrador

        prestamoEdit.setCodigo(Long.valueOf(codigo));
        prestamoEdit.setMonto(Double.valueOf(monto));
        prestamoEdit.setPlazo(Integer.valueOf(plazo));
        prestamoEdit.setNeto(Double.valueOf(neto));
        prestamoEdit.setInteres(Double.valueOf(interes));
        prestamoEdit.setValorCuota(Double.valueOf(valorCuota));
        prestamoEdit.setObservacion(observacion);
        prestamoEdit.setFecha(fecha);
        prestamoEdit.setModoPago(modo);

        boolean actualizo = mP.actualizarPrestamo(prestamoEdit);//actualizamos el prestamo
        if (actualizo) {
            JOptionPane.showMessageDialog(null, "Prestamo actualizado correctamente", "Actualizar Prestamo", JOptionPane.INFORMATION_MESSAGE);
            botonEliminar.setEnabled(false);
            modificarPrestamo.setEnabled(false);
            guardarPrestamo.setEnabled(true);
        } else {
            JOptionPane.showMessageDialog(null, "Error al actualizar el prestaamo", "Actualizar Prestamo", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_modificarPrestamoActionPerformed

    private void botonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEliminarActionPerformed
        // TODO add your handling code here:
        if (prestamoEdit != null) {
            boolean elimino = false;
            int numDatosUpdate = 0;
            ManejadorDatosFinancieros mDf = new ManejadorDatosFinancieros();
            int r = JOptionPane.showConfirmDialog(this, " Desea Realmente Eliminar este Prestamo ", "Advertencia", JOptionPane.YES_NO_OPTION);
            if (r == JOptionPane.YES_OPTION) {
//                Prestamo p = mP.consultarPrestamoById(prestamoEdit.getId().toString());
                Prestamo p=prestamoEdit;

                if (p != null) {
                    for (Pago pago : p.getPagos()) {
                        Date fecha = pago.getFecha();
                        DatosFinanciero df = mDf.DatoFinancieroByFecha(fecha);
                        Double recaudoDato = df.getRecaudo();
                        Double cuatoArestar = pago.getAbono();
                        recaudoDato -= cuatoArestar;
                        df.setRecaudo(recaudoDato);
                        mDf.actualizarDatoFinanciero(df);
                        numDatosUpdate++;
                    }


                    if (numDatosUpdate == p.getPagos().size()) {
                        elimino = mP.eliminarPrestamo(prestamoEdit.getId());
                    }
                }

                if (elimino) {
                    JOptionPane.showMessageDialog(null, "Prestamo eliminado correctamente", "Actualizar Prestamo", JOptionPane.INFORMATION_MESSAGE);
                    botonEliminar.setEnabled(false);
                    modificarPrestamo.setEnabled(false);
                    guardarPrestamo.setEnabled(true);
                    DatosFinanciero df=mDf.UltimoDatoFinanciero();
                     VentanaPrincipal.totalSesion = df.getRecaudo();
                     VentanaPrincipal.recaudoHoy.setText(String.valueOf(df.getRecaudo()));
                    limpiar();
                }
            } else {
                JOptionPane.showMessageDialog(null, "Prestamo no Eliminado", "Eliminar Prestamo", JOptionPane.INFORMATION_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Prestamo no Encontrado", "Eliminar Prestamo", JOptionPane.INFORMATION_MESSAGE);
        }

    }//GEN-LAST:event_botonEliminarActionPerformed

    public void cargarCobrador() {
        ManejadorCobrador mCo = new ManejadorCobrador();
        List<Cobrador> cobradores = mCo.todosLosCobradores();
        AutoCompleteDecorator.decorate(comboCobrador);
        comboCobrador.addItem("Seleccione un cobrador");
        for (Cobrador cobrador : cobradores) {
            comboCobrador.addItem(cobrador);

        }
    }

    public void limpiar() {
        campoCodigo.setText("");
        campoNombre.setText("");
        campoCedula.setText("");
        campoTelefono.setText("");
        areaDireccion.setText("");
        areaObservacion.setText("");
        areaNotificacion.setText("");
        campoPorcentaje.setText("");
        campoMonto.setText("");
        campoMontoPagar.setText("");
        campoDias.setText("0");
        campoValorDia.setText("0");
        comboTipoPago.setSelectedIndex(0);
//        comboCobrador.setSelectedIndex(0);

    }

    public void limpiarDatosPrestamo() {
//        campoMonto.setText("");
//        campoMontoPagar.setText("");
//        campoDias.setText("0");
//        campoValorDia.setText("0");
//        comboTipoPago.setSelectedIndex(0);
    }

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                VentanaPersona dialog = new VentanaPersona(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    public void inicializar() {
        campoIdCliente.setVisible(false);
        modificarPrestamo.setEnabled(false);
        botonEliminar.setEnabled(false);
        mP = new ManejadorPrestamo();

        idUltimoPrestamo.setText("");
        cargarCobrador();//cargar los cobradores
        ComboBoxModel cbModel = new DefaultComboBoxModel(Prestamo.ModoPago.values());
        comboTipoPago.setModel(cbModel);
        Long idPrestamoUlti = mP.idUltimoPrestamo();
        if (idPrestamoUlti != null) {
            idUltimoPrestamo.setText(String.valueOf(idPrestamoUlti));
        }
        labelCedula.setDisplayedMnemonic('c');
        campoCedula.setFocusAccelerator('c');

        labelCodigoPrestamo.setDisplayedMnemonic('d');
        campoCodigo.setFocusAccelerator('d');

        labelMonto.setDisplayedMnemonic('m');
        campoMonto.setFocusAccelerator('m');

        labelPorcentaje.setDisplayedMnemonic('p');
        campoPorcentaje.setFocusAccelerator('p');

        labelDias.setDisplayedMnemonic('d');
        campoDias.setFocusAccelerator('d');

        labelObservacion.setDisplayedMnemonic('o');
        areaObservacion.setFocusAccelerator('o');


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JTextArea areaDireccion;
    private javax.swing.JTextArea areaNotificacion;
    private javax.swing.JTextArea areaObservacion;
    private javax.swing.JButton botonEliminar;
    private javax.swing.JButton buscarCliente;
    public static javax.swing.JTextField campoCedula;
    private javax.swing.JTextField campoCodigo;
    private javax.swing.JTextField campoDias;
    private org.jdesktop.swingx.JXDatePicker campoFecha;
    public static javax.swing.JTextField campoIdCliente;
    private javax.swing.JTextField campoMonto;
    private javax.swing.JTextField campoMontoPagar;
    public static javax.swing.JTextField campoNombre;
    private javax.swing.JTextField campoPorcentaje;
    public static javax.swing.JTextField campoTelefono;
    private javax.swing.JTextField campoValorDia;
    private javax.swing.JButton clientesList;
    private javax.swing.JComboBox comboCobrador;
    private javax.swing.JComboBox comboTipoPago;
    private javax.swing.JButton guardarPrestamo;
    private javax.swing.JTextField idUltimoPrestamo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXPanel jXPanel2;
    private org.jdesktop.swingx.JXPanel jXPanel3;
    private javax.swing.JLabel labelCedula;
    private javax.swing.JLabel labelCodigoPrestamo;
    private javax.swing.JLabel labelDias;
    private javax.swing.JLabel labelMonto;
    private javax.swing.JLabel labelObservacion;
    private javax.swing.JLabel labelPorcentaje;
    private javax.swing.JButton modificarPrestamo;
    private org.jdesktop.swingx.JXPanel panelCliente;
    private org.jdesktop.swingx.JXPanel panelPrestamo;
    // End of variables declaration//GEN-END:variables
}
